import Vue from "vue";
import Vuex from "vuex";
import store from "storejs";
Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    gedan: [
      { id: 144277359, name: "孤独、忧怨、哀伤、迷幻、飘渺、激昂、平静" },
      { id: 2511154, name: "B站收藏动漫曲年度排行榜1-50（经典 童年)" },
      { id: 49389984, name: "在那一瞬间，我仿佛听见了全世界崩溃的声音" },
      { id: 487403213, name: "ACG中的震撼，源于对心灵最纯粹的触动！" },
    ],
    playlist:
      !store.get("listhistory") || store.get("listhistory").length == 0
        ? []
        : [
            {
              id: store.get("listhistory")[0].id,
              name: store.get("listhistory")[0].Name,
              songName: store.get("listhistory")[0].songName,
            },
          ], //播放列表
    Nowplay:
      !store.get("listhistory") || store.get("listhistory").length == 0
        ? ""
        : store.get("listhistory")[0].id, //当前播放歌曲
    history: store.get("listhistory") || [], //历史
    showsongdetail: false, //显示歌曲详情
    audio: "", //播放器对象
    play: false, //播放状态
    ref: "",
    loves: store.get("love") || [
      {
        id: 346576,
        name: "Beyond",
        songname: "光辉岁月",
        time: 300000,
        zj: "光辉岁月十五年",
      },
    ], //我喜欢的音乐
    songs: [
      {
        id: "5781",
        img:
          "https://p2.music.126.net/W__FCWFiyq0JdPtuLJoZVQ==/109951163765026271.jpg",
        name: "薛之谦",
        zj: 17,
        mv: 79,
      },
      {
        id: "6452",
        img:
          "https://p2.music.126.net/ql3nSwy0XKow_HAoZzRZgw==/109951163111196186.jpg",
        name: "周杰伦",
        zj: 37,
        mv: 8,
      },
    ],
    zj: [
      {
        id: "2681139",
        name: "意外",
        img:
          "https://p2.music.126.net/WPHmBisDxnoF4DrBLKwl3Q==/109951163169021112.jpg",
        songname: "薛之谦",
        num: 10,
      },
      {
        id: "18896",
        name: "十一月的萧邦",
        img:
          "https://p1.music.126.net/yEGx5yAd9zaa3l03d30KNw==/18511377767183740.jpg",
        songname: "周杰伦",
        num: 12,
      },
    ], //专辑
  },
  mutations: {
    addgedan(state, gedan) {
      //收藏歌单
      state.gedan.push(gedan);
    },
    delgedan(state, id) {
      let del = state.gedan.findIndex((item) => item.id == id);
      del != -1 ? state.gedan.splice(del, 1) : null;
    },
    addPlayList(state, list) {
      //添加播放列表
      var nowDate = new Date().getTime();
      let history = {
        id: list.id,
        songName: list.songName,
        Name: list.name,
        time: nowDate,
      };
      let newh = state.history;
      if (newh) {
        let find =
          store.get("listhistory") &&
          store.get("listhistory").find((item) => item.id == history.id);
        if (!find) {
          newh.unshift(history);
          store.set("listhistory", newh);
        }
      } else {
        store.set("listhistory", history);
      }
      if (state.playlist.length == 0) {
        state.playlist.push(list);
      } else {
        let findId = state.playlist.find((item) => item.id == list.id);
        !findId ? state.playlist.push(list) : null;
      }
    },
    playAll(state, playlist) {
      //播放全部
      let { list } = playlist;
      if (playlist.type == "replce") {
        state.playlist = list;
        state.Nowplay = list[0].id;
      } else {
        state.Nowplay ? null : (state.Nowplay = list[0].id);
        state.playlist = state.playlist.concat(list);
      }
    },
    delPlayList(state, obj) {
      //删除播放列表
      if (obj.type == "all") {
        state.playlist = [];
       // state.play = false;
         
        //state.Nowplay=null; 
      } else {
        let del = state.playlist.findIndex((item) => item.id == obj.id);
        del != -1 ? state.playlist.splice(del, 1) : null;
      }
    },
    play(state, id) {
      state.Nowplay = id;
    },
    plushplay(state, list) {
      let newh = store.get("listhistory") || [];
      var nowDate = new Date().getTime();
      let history = {
        id: list.id,
        songName: list.songName,
        Name: list.name,
        time: nowDate,
      };
      if (state.history.length == 0) {
        newh.unshift(history);
        store.set("listhistory", newh);
      } else {
        let a = newh.some((item) => item.id == list.id);
        if (!a) {
          newh.unshift(history);
          state.history = newh;
          store.set("listhistory", newh);
        }
      }
      state.Nowplay = list.id;
    },
    delHis(state, id) {
      //删除播放历史
      if (id) {
        let del = state.history.findIndex((item) => item.id == id);
        del != -1 ? state.history.splice(del, 1) : null;
        store.set("listhistory", state.history);
      } else {
        state.history = [];
        store.set("listhistory", []);
      }
    },
    showsong(state) {
      state.showsongdetail = !state.showsongdetail;
    },
    initAudio(state, audio) {
      state.audio = audio;
    },
    changeplay(state, play) {
      state.play = play;
    },
    addref(state, ref) {
      state.ref = ref;
    },
    addlove(state, love) {
      let ok = state.loves.some((item) => item.id == love.id);
      if (!ok) {
        state.loves.push(love);
        store.set("love", state.loves);
      }
    },
    dellove(state, id) {
      const index = state.loves.findIndex((item) => item.id == id);
      state.loves.splice(index, 1);
      store.set("love", state.loves);
    },
    addsongs(state, info) {
      state.songs.push(info);
    },
    delsongs(state, id) {
      const index = state.songs.findIndex((item) => item.id == id);
      state.songs.splice(index, 1);
    },
    addzj(state, zj) {
      state.zj.push(zj);
    },
    delzj(state, id) {
      const index = state.zj.findIndex((item) => item.id == id);
      state.zj.splice(index, 1);
    },
  },
  actions: {},
  modules: {},
});
